package com.eiisan.web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.eiisan.core.token.GenerateToken;
import com.eiisan.core.util.BeanUtil;
import com.eiisan.core.util.api.Result;
import com.eiisan.web.factory.StrategyFactory;
import com.eiisan.web.mapper.PaymentChannelMapper;
import com.eiisan.web.mapper.PaymentTransactionMapper;
import com.eiisan.web.model.domain.PaymentChannel;
import com.eiisan.web.model.domain.PaymentTransaction;
import com.eiisan.web.model.dto.PayMentTransacDTO;
import com.eiisan.web.service.PayMentTransacService;
import com.eiisan.web.strategy.PayStrategy;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 支付交易实现
 * @author gragonfly
 * @date 2019/7/17
 **/
@Service
public class PayMentTransacServiceImpl implements PayMentTransacService {

    @Autowired
    private GenerateToken generateToken;
    @Autowired
    private PaymentTransactionMapper paymentTransactionMapper;
    @Autowired
    private PaymentChannelMapper paymentChannelMapper;

    @Override
    public Result<PayMentTransacDTO> tokenByPayMentTransac(String token) {
        // 1.验证token是否为空
        if (StringUtils.isEmpty(token)) {
            return Result.errorRequest("token参数不能空!");
        }
        // 2.使用token查询redisPayMentTransacID
        String value = generateToken.getToken(token);
        if (StringUtils.isEmpty(value)) {
            return Result.fail("该Token可能已经失效或者已经过期");
        }
        // 3.转换为整数类型
        Long transactionId = Long.parseLong(value);
        // 4.使用transactionId查询支付信息
        PaymentTransaction paymentTransaction = paymentTransactionMapper.selectById(transactionId);
        if (paymentTransaction == null) {
            return Result.fail("未查询到该支付信息");
        }
        return Result.success(BeanUtil.doToDto(paymentTransaction, PayMentTransacDTO.class));
    }

    @Override
    public Result<JSONObject> toPayHtml(String channelId, String payToken) {
        // 1.使用渠道id获取渠道信息 classAddres
        PaymentChannel pymentChannel = paymentChannelMapper.selectBychannelId(channelId);
        if (pymentChannel == null) {
            return Result.fail("没有查询到该渠道信息");
        }
        // 2.使用payToken获取支付参数
        Result<PayMentTransacDTO> tokenByPayMentTransac = tokenByPayMentTransac(payToken);
        if (Result.isNotSuccess(tokenByPayMentTransac)) {
            return Result.fail(tokenByPayMentTransac.getMessage());
        }
        PayMentTransacDTO payMentTransacDTO = tokenByPayMentTransac.getData();
        // 3.执行具体的支付渠道的算法获取html表单数据 策略设计模式 使用java反射机制 执行具体方法
        String classAddres = pymentChannel.getClassAddres();
        PayStrategy payStrategy = StrategyFactory.getPayStrategy(classAddres);
        String payHtml = payStrategy.toPayHtml(pymentChannel, payMentTransacDTO);
        // 4.直接返回html
        JSONObject data = new JSONObject();
        data.put("payHtml", payHtml);
        return Result.success(data);
    }
}
